using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using System.Windows.Forms;

namespace XTYAPI.文件
{
    public class Com
    {
        public const string dateFormat1 = "HH:mm:ss";
        public const string dateFormat2 = "yyyy / MM / dd HH:mm:ss";
        public const string dateFormat3 = "yyyy-MM-dd";


        public static string AppStartPath { get { return Environment.CurrentDirectory; } }
        public static string LogErrorFilePath { get { return AppStartPath + "\\LogFile\\Debug\\"; } }
        public static string LoadFile(string path)
        {
            string cfg = string.Empty;
#if Debug
           
#endif
            FileStream fs = null;
            StreamReader sr = null;
            try
            {
                fs = new FileStream(path, FileMode.Open);
                sr = new StreamReader(fs, Encoding.Default);
                cfg = sr.ReadToEnd();
                sr.Close();
                fs.Close();
                return cfg;
            }
            catch (Exception ex)
            {
                return cfg;
                throw new Exception(ex.Message);
                //return string.Empty;
            }
            finally
            {
                sr?.Close();
                fs?.Close();

            }
        }
        public static bool SaveFile(string path, string cfg, FileMode flieMode = FileMode.Create)
        {
            FileStream fs = null;
            StreamWriter sw = null;
            try
            {
                fs = new FileStream(path, flieMode);
                sw = new StreamWriter(fs, Encoding.Default);
                sw.Write(cfg);
                sw.Close();
                fs.Close();
                return true;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
                //return false;
            }
            finally
            {
                sw?.Close();
                fs?.Close();
            }
        }
        public static bool LoadFile<T>(string path, ref T t1) where T : new()
        {
            string result = LoadFile(path);
            if (result == null || result == "null" || result == string.Empty)
            {
                goto L1;
            }
            t1 = JsonConvert.DeserializeObject<T>(result);
            if (t1 == null)
            {
                goto L1;
            }
            return true;
            L1:
            return false;

        }
        public static string GetPath(string path)
        {
            return path.Substring(0,path.LastIndexOf("\\"));//最后一个\后的数据
        }
        public static string GetPathFile(string path)
        {
          return  path.Substring(path.LastIndexOf("\\") + 1);//最后一个\后的数据
        }
        public static string ChooseSaveFile(string title, string initFolder)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = title;
            /*dlg.FileName = "User.json"; */// Default file name
            dlg.DefaultExt = ".json"; // Default file extension
            dlg.Filter = "Json工艺文件|*.json"; // Filter files by extension
            dlg.InitialDirectory = initFolder;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                return dlg.FileName;
            }
            else
            {
                return string.Empty;
            }
        }
        public static T CopyObj<T>(T t1)
        {
            string obj = JsonConvert.SerializeObject(t1);
            return JsonConvert.DeserializeObject<T>(obj);
        }
        public static string SerializeObject<T>(T t1)
        {
            return JsonConvert.SerializeObject(t1);
        }
        public static T DeserializeObject<T>(string obj)
        {
            return JsonConvert.DeserializeObject<T>(obj);
        }
    }
}
